package xuhaijia.lanou3g.autohome.find;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.NetTool;

/**
 * Created by 7 on 16/8/22.
 */
public class FunctionAdapter extends RecyclerView.Adapter<FunctionAdapter.FunctionViewHolder>{
    private Context context;
    private FunctionBean functionBean;
    FunctionClickListener functionClickListener;

    public void setFunctionClickListener(FunctionClickListener functionClickListener) {
        this.functionClickListener = functionClickListener;
    }

    public FunctionAdapter(Context context) {
        this.context = context;
    }

    public void setFunctionBean(FunctionBean functionBean) {
        this.functionBean = functionBean;
        notifyDataSetChanged();
    }

    @Override
    public FunctionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_rv_function , parent , false);
        FunctionViewHolder functionViewHolder = new FunctionViewHolder(view);
        return functionViewHolder;
    }

    @Override
    public void onBindViewHolder(FunctionViewHolder holder, final int position) {
        NetTool mNetTool = new NetTool();
        mNetTool.getImage(functionBean.getResult().getFunctionlist().get(position).getIconurl() , holder.iconIv);
        holder.nameTv.setText(functionBean.getResult().getFunctionlist().get(position).getTitle());
        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                functionClickListener.onItemClick(position);
            }
        });

    }


    @Override
    public int getItemCount() {
        if (functionBean.getResult().getFunctionlist() == null) {
            return 0;
        }
        return functionBean.getResult().getFunctionlist().size();
    }



    public class FunctionViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        ImageView iconIv;
        LinearLayout ll;
        public FunctionViewHolder(View itemView) {
            super(itemView);
            ll = (LinearLayout) itemView.findViewById(R.id.item_rv_function_ll);
            nameTv = (TextView) itemView.findViewById(R.id.item_rv_function_tv);
            iconIv = (ImageView) itemView.findViewById(R.id.item_rv_function_iv);
        }
    }

    public interface FunctionClickListener {
        void onItemClick(int position);
    }
}
